package xkp.lesson5.file;



import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestNIOCopy {
    public static void main(String[] args) throws IOException {
        copyFile("C:\\Users\\Administrator\\Desktop\\apache-tomcat-9.0.29-windows-x64.zip",
                "Lesson30/file");
    }

    public static void copyFile(String filePath,String destDir) throws IOException {
        File file = new File(filePath);
        FileChannel srcChannel = new FileInputStream(file).getChannel();
        FileChannel descChanel = new FileOutputStream(
                destDir+File.separator+file.getName()).getChannel();


        //1、transferFrom 从谁里面拷贝到我里面
        //descChanel.transferFrom(srcChannel,0,srcChannel.size());
        //2、transferTo  把我里面的数据拷贝到谁里面
        srcChannel.transferTo(0,srcChannel.size(),descChanel);
        descChanel.force(true);
        descChanel.close();
        srcChannel.close();
    }


    @Test
    public void testNioCopy() throws IOException {
        copyFile("C:\\Users\\Administrator\\Desktop\\apache-tomcat-9.0.29-windows-x64.zip",
                "Lesson30/file");
    }
}
